package cn.bgs.serviceedu.controller;


import cn.bgs.serviceedu.entity.Category;
import cn.bgs.serviceedu.entity.Hospital;
import cn.bgs.serviceedu.service.HospitalService;
import cn.bgs.serviceutils.utils.CSException;
import cn.bgs.serviceutils.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 医院表 前端控制器
 * </p>
 *
 * @author YiChen
 * @since 2021-10-20
 */
@Api(description = "医院表")
@RestController
@RequestMapping("/serviceedu/hospital")
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;



    @ApiOperation(value = "分页模糊查询")
    @PostMapping("/findAll")
    @CachePut(key = "'HospitalList'",value = "HospitalList")
    public R findAll(@RequestBody Hospital hospital){
        LambdaQueryWrapper<Hospital> wrapper = Wrappers.<Hospital>lambdaQuery()
                .like(StringUtils.isNoneBlank(hospital.getHospitalName()),Hospital::getHospitalName,StringUtils.trimToEmpty(hospital.getHospitalName()))
                .eq(Hospital::getDeleteFlag,0)
                .orderByAsc(Hospital::getCreateTime);
        return R.ok().data("list",hospitalService.list(wrapper));
    }


    /**
     * 根据ID删除
     * @param hospitalId
     * @return
     */
    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("/del/{hospitalId}")
    @CachePut(key = "'HospitalList'",value = "HospitalList")
    public R del(@PathVariable Integer hospitalId){
        boolean b = false;
        try {
            b = hospitalService.removeById(hospitalId);
        } catch (Exception e) {
            throw new CSException(20001,"删除失败");
        }
        if (b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }


    /**
     * 根据ID单查
     * @param hospitalId
     * @return
     */
    @ApiOperation(value = "根据ID单查")
    @GetMapping("/findById/{hospitalId}")
    public R findById(@PathVariable Integer hospitalId){
        return   R.ok().data("list",hospitalService.getById(hospitalId));
    }


    /**
     * 添加用户
     * @param category
     * @return
     */
    @ApiOperation(value = "添加医院")
    @PostMapping("/addHospital")
    @CachePut(key = "'HospitalList'",value = "HospitalList")
    public R addUser(@RequestBody Hospital category ){

        boolean save = false;
        try {
            save = hospitalService.save(category);
        } catch (Exception e) {
            throw new CSException(20001,"添加失败");
        }
        if (save){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }


    /**
     * 修改用户
     * @param category
     * @return
     */
    @ApiOperation(value = "修改医院")
    @PutMapping("/update")
    @CachePut(key = "'HospitalList'",value = "HospitalList")
    public R update(@RequestBody Hospital category){
        boolean update = false;
        try {
            update = hospitalService.updateById(category);
        } catch (Exception e) {
            throw new CSException(20001,"修改失败");
        }
        if (update){
            return  R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }


}
